﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using LibKetoan;
using System.Data;

namespace Ketoan
{
	/// <summary>
	/// Summary description for Frmbctaichinh.
	/// </summary>
	public class Frmbctaichinh : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox grbThang;
		private System.Windows.Forms.NumericUpDown numNam;
		private System.Windows.Forms.ComboBox cboQuy;
		private System.Windows.Forms.ComboBox cboChon;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label lblThoiGian;
		private System.Windows.Forms.ComboBox cboThang;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button butExcel;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Button butXem;
		private System.Windows.Forms.ComboBox cbobaocao;
		private System.Windows.Forms.Label label2;
		private System.Data.DataSet ds =new DataSet();
		private AccessData a;
		private int i_userid;
		private string text="",text1="",s_Report="",s_Tenform="",Madvqhns="",ngayin="",ngaythangnam="";
		private DataSet ds_Data=new DataSet();		
		private bool b_thang=true,b_quy=true,b_nam=true;
		private string s_tam="";	
		private exportExcel exportExcel=new exportExcel();


		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Frmbctaichinh(AccessData acc,int userid)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;
			i_userid=userid;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Frmbctaichinh));
            this.grbThang = new System.Windows.Forms.GroupBox();
            this.numNam = new System.Windows.Forms.NumericUpDown();
            this.cboQuy = new System.Windows.Forms.ComboBox();
            this.cboChon = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.lblThoiGian = new System.Windows.Forms.Label();
            this.cboThang = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.butExcel = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butXem = new System.Windows.Forms.Button();
            this.cbobaocao = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.grbThang.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numNam)).BeginInit();
            this.SuspendLayout();
            // 
            // grbThang
            // 
            this.grbThang.Controls.Add(this.numNam);
            this.grbThang.Controls.Add(this.cboQuy);
            this.grbThang.Controls.Add(this.cboChon);
            this.grbThang.Controls.Add(this.label1);
            this.grbThang.Controls.Add(this.lblThoiGian);
            this.grbThang.Controls.Add(this.cboThang);
            this.grbThang.Controls.Add(this.label3);
            this.grbThang.Dock = System.Windows.Forms.DockStyle.Top;
            this.grbThang.Location = new System.Drawing.Point(0, 0);
            this.grbThang.Name = "grbThang";
            this.grbThang.Size = new System.Drawing.Size(440, 48);
            this.grbThang.TabIndex = 187;
            this.grbThang.TabStop = false;
            // 
            // numNam
            // 
            this.numNam.ForeColor = System.Drawing.Color.DarkBlue;
            this.numNam.Location = new System.Drawing.Point(328, 16);
            this.numNam.Maximum = new decimal(new int[] {
            2900,
            0,
            0,
            0});
            this.numNam.Minimum = new decimal(new int[] {
            2009,
            0,
            0,
            0});
            this.numNam.Name = "numNam";
            this.numNam.Size = new System.Drawing.Size(48, 20);
            this.numNam.TabIndex = 24;
            this.numNam.Value = new decimal(new int[] {
            2009,
            0,
            0,
            0});
            // 
            // cboQuy
            // 
            this.cboQuy.ForeColor = System.Drawing.Color.DarkBlue;
            this.cboQuy.Items.AddRange(new object[] {
            "1",
            "2",
            "3",
            "4"});
            this.cboQuy.Location = new System.Drawing.Point(208, 16);
            this.cboQuy.Name = "cboQuy";
            this.cboQuy.Size = new System.Drawing.Size(64, 21);
            this.cboQuy.TabIndex = 23;
            // 
            // cboChon
            // 
            this.cboChon.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboChon.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cboChon.ForeColor = System.Drawing.Color.DarkBlue;
            this.cboChon.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.cboChon.Items.AddRange(new object[] {
            " Tháng",
            " Quý",
            " Năm"});
            this.cboChon.Location = new System.Drawing.Point(88, 16);
            this.cboChon.Name = "cboChon";
            this.cboChon.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.cboChon.Size = new System.Drawing.Size(72, 21);
            this.cboChon.TabIndex = 19;
            this.cboChon.SelectedIndexChanged += new System.EventHandler(this.cboChon_SelectedIndexChanged);
            // 
            // label1
            // 
            this.label1.BackColor = System.Drawing.SystemColors.Control;
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(8, 16);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(80, 23);
            this.label1.TabIndex = 18;
            this.label1.Text = "Báo cáo theo :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lblThoiGian
            // 
            this.lblThoiGian.BackColor = System.Drawing.SystemColors.Control;
            this.lblThoiGian.ForeColor = System.Drawing.Color.DarkBlue;
            this.lblThoiGian.Location = new System.Drawing.Point(152, 16);
            this.lblThoiGian.Name = "lblThoiGian";
            this.lblThoiGian.Size = new System.Drawing.Size(40, 23);
            this.lblThoiGian.TabIndex = 22;
            this.lblThoiGian.Text = "Tháng";
            this.lblThoiGian.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // cboThang
            // 
            this.cboThang.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboThang.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cboThang.ForeColor = System.Drawing.Color.DarkBlue;
            this.cboThang.ImeMode = System.Windows.Forms.ImeMode.NoControl;
            this.cboThang.Items.AddRange(new object[] {
            "01",
            "02",
            "03",
            "04",
            "05",
            "06",
            "07",
            "08",
            "09",
            "10",
            "11",
            "12",
            "6 tháng đầu năm",
            "6 tháng cuối năm"});
            this.cboThang.Location = new System.Drawing.Point(192, 16);
            this.cboThang.Name = "cboThang";
            this.cboThang.RightToLeft = System.Windows.Forms.RightToLeft.No;
            this.cboThang.Size = new System.Drawing.Size(104, 21);
            this.cboThang.TabIndex = 19;
            // 
            // label3
            // 
            this.label3.BackColor = System.Drawing.SystemColors.Control;
            this.label3.ForeColor = System.Drawing.Color.DarkBlue;
            this.label3.Location = new System.Drawing.Point(288, 16);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(40, 23);
            this.label3.TabIndex = 22;
            this.label3.Text = "Năm";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // butExcel
            // 
            this.butExcel.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.butExcel.BackColor = System.Drawing.SystemColors.Control;
            this.butExcel.ForeColor = System.Drawing.SystemColors.WindowText;
            this.butExcel.Image = ((System.Drawing.Image)(resources.GetObject("butExcel.Image")));
            this.butExcel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butExcel.Location = new System.Drawing.Point(175, 81);
            this.butExcel.Name = "butExcel";
            this.butExcel.Size = new System.Drawing.Size(68, 25);
            this.butExcel.TabIndex = 185;
            this.butExcel.Text = "     &Excel";
            this.butExcel.UseVisualStyleBackColor = false;
            this.butExcel.Click += new System.EventHandler(this.butExcel_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.butKetthuc.BackColor = System.Drawing.SystemColors.Control;
            this.butKetthuc.ForeColor = System.Drawing.SystemColors.WindowText;
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(246, 81);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(68, 25);
            this.butKetthuc.TabIndex = 186;
            this.butKetthuc.Text = "&Kết thúc";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.UseVisualStyleBackColor = false;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butXem
            // 
            this.butXem.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.butXem.BackColor = System.Drawing.SystemColors.Control;
            this.butXem.ForeColor = System.Drawing.SystemColors.WindowText;
            this.butXem.Image = ((System.Drawing.Image)(resources.GetObject("butXem.Image")));
            this.butXem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butXem.Location = new System.Drawing.Point(105, 81);
            this.butXem.Name = "butXem";
            this.butXem.Size = new System.Drawing.Size(68, 25);
            this.butXem.TabIndex = 184;
            this.butXem.Text = "     &Xem";
            this.butXem.UseVisualStyleBackColor = false;
            this.butXem.Click += new System.EventHandler(this.butXem_Click);
            // 
            // cbobaocao
            // 
            this.cbobaocao.ForeColor = System.Drawing.Color.DarkBlue;
            this.cbobaocao.Location = new System.Drawing.Point(88, 54);
            this.cbobaocao.Name = "cbobaocao";
            this.cbobaocao.Size = new System.Drawing.Size(288, 21);
            this.cbobaocao.TabIndex = 183;
            this.cbobaocao.SelectedIndexChanged += new System.EventHandler(this.cbobaocao_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(8, 54);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(80, 23);
            this.label2.TabIndex = 182;
            this.label2.Text = "Xem báo cáo:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // Frmbctaichinh
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(440, 113);
            this.Controls.Add(this.grbThang);
            this.Controls.Add(this.butExcel);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.butXem);
            this.Controls.Add(this.cbobaocao);
            this.Controls.Add(this.label2);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "Frmbctaichinh";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Frmbctaichinh";
            this.Load += new System.EventHandler(this.Frmbctaichinh_Load);
            this.grbThang.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.numNam)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		private void Frmbctaichinh_Load(object sender, System.EventArgs e)
		{
			try
			{
				cboChon.SelectedIndex =0;
				cboThang.SelectedValue=DateTime.Now.Month.ToString();
				cboQuy.SelectedIndex=0;

				ds=a.get_maubc();		
				cbobaocao.DataSource=ds.Tables[0];
				cbobaocao.DisplayMember="TEN";
				cbobaocao.ValueMember="MAUBIEU";
				loadform();
			}
			catch{}
		{}
		}
		public void loadform()
		{
			switch (cbobaocao.SelectedValue.ToString())
			{				
				case "B05-H":
					this.Text="Báo Cáo Thu Chi Hoạt Động Sự nghiệp & Sản Xuất Kinh Doanh ";
					s_Tenform="ThuChiHoatDongSuNghiepSXKD";
					s_Report="KT_BaoCaoThuChiHoatDongSuNghiep_SXKD.rpt";
					butXem.Enabled=butExcel.Enabled=butKetthuc.Enabled=true;
					break;	
				case "B06-H":
					this.Text="Thuyết minh báo cáo tài chính";
					s_Tenform="Baocaotaichinh";
                    s_Report = "KT_Baocaotaichinh.rpt";
					butXem.Enabled=butExcel.Enabled=butKetthuc.Enabled=true;
					break;	
				case "B03-H":
					this.Text="Báo cáo số kinh phí chưa sử dụng đã quyết toán năm trước chuyển sang";
					s_Tenform="BaocaoKPchuasudung";
					s_Report="KT_BaoCaoKPChuaSuDung.rpt";
					butXem.Enabled=butExcel.Enabled=butKetthuc.Enabled=true;
					break;				
				default:
					break;
			}
		}

		private void Display(int loai)
		{			
			string yy=Convert.ToString(numNam.Value.ToString());
			string mm1="",mm2="";
			//Tinh lai cac gia tri thang theo dieu kien chon ngay thang.
			if(cboChon.SelectedIndex==0)
			{
				b_thang=false;
				b_quy=true;
				b_nam=true;
				switch (cboThang.SelectedIndex)
				{
					case 0 : mm1=mm2="01"; break;
					case 1 : mm1=mm2="02"; break;
					case 2 : mm1=mm2="03"; break;
					case 3 : mm1=mm2="04"; break;
					case 4 : mm1=mm2="05"; break;
					case 5 : mm1=mm2="06"; break;
					case 6 : mm1=mm2="07"; break;
					case 7 : mm1=mm2="08"; break;
					case 8 : mm1=mm2="09"; break;
					case 9 : mm1=mm2="10"; break;
					case 10: mm1=mm2="11"; break;
					case 11: mm1=mm2="12"; break;
					case 12: 
						mm1="01";
						mm2="06";
						break;
					case 13: 
						mm1="07";
						mm2="12";
						break;
					default: 
						break;
				}
			}
			else if(cboChon.SelectedIndex==1)
			{
				b_quy=false;
				b_thang=true;
				b_nam=true;
				switch (cboQuy.SelectedIndex)
				{				
					case 0 : 
						mm1="01"; 
						mm2="03";
						break;
					case 1 : 
						mm1="04"; 
						mm2="06";
						break;
					case 2 : 
						mm1="07"; 
						mm2="09";
						break;
					case 3 : 
						mm1="10"; 
						mm2="12";
						break;
					default: 
						break;
				}
			}
			else
			{
				b_nam=false;
				b_quy=true;
				b_thang=true;
				mm1="01";
				mm2="12";
			}			
			string s_mm1=mm1.ToString().PadLeft(2,'0');
			string s_mm2=mm2.ToString().PadLeft(2,'0');
			string s_yyyy=yy.ToString().Trim();
			string s_yy=s_yyyy.Substring(2,2);
			string s_Thangnam=a.kt_Tinhsodu(s_mm2.ToString(),s_yy);
			if (s_Thangnam!="")
			{
				MessageBox.Show("Các tháng "+s_Thangnam.Substring(0,s_Thangnam.Length-1)+" chưa tính số dư!","Chú ý!");
				//xac minh co tinh so du
				if (MessageBox.Show ("Bạn có muốn kiểm tra số dư không ?",this.Text,MessageBoxButtons.YesNo, MessageBoxIcon.Question)== DialogResult.Yes)
				{
					//tinh so du
					string sql="",_tu="",_den="";
					_den=s_mm2+s_yyyy.Substring(2,2);			
					//lay thang tu						
					sql="select mmyy from (select * from a_dtsd where substr(trim(mmyy),3,4)='"+s_yy+"' order by mmyy asc) where rownum=1";
					DataSet ds=new DataSet();
					ds=a.get_data(sql);
					if(ds!=null && ds.Tables[0].Rows.Count>0)
						_tu=ds.Tables[0].Rows[0]["mmyy"].ToString();
					else _tu=s_mm1+s_yyyy.Substring(2,2);			
					if (!a.bYyyy(s_yyyy))
					{
						MessageBox.Show("Số liệu năm "+s_yyyy+" chưa tạo !",a.Msg);
						return;
					}
					string s=a.KiemtraFb(_tu,_den);
					if (s!="")
					{
						MessageBox.Show("Số liệu tháng :"+s.Substring(0,s.Length-1)+" Năm "+s_yyyy+" bị khóa !");    		
						return;
					}
					else 
					{
						Cursor=Cursors.WaitCursor;
						a.KiemtraFb(_tu,_den,i_userid);
						Cursor=Cursors.Default;
					}
					//a.updSodu(_tu,a.Mmyy_sau(_den),i_Iduser,false);
					MessageBox.Show("Đã kiểm tra số dư cuối tháng "+_tu.Substring(0,2)+" đến tháng "+_den.Substring(0,2)+" năm 20"+s_yy+" !",a.Msg);					
					//
				}
				return;
			}

			if (cboChon.SelectedIndex ==2)text = "Năm " + numNam.Value.ToString();
			if (cboChon.SelectedIndex ==0)text = "Tháng " + mm1 + " Năm " + s_yyyy;
			{
				if (cboThang.SelectedIndex ==12)text = "Tháng 01 đến tháng 06 Năm " + s_yyyy;
				else if (cboThang.SelectedIndex ==13) text = "Tháng 07 đến tháng 12 Năm " + s_yyyy;
				else text = "Tháng " + mm1 + " Năm " + s_yyyy;
			}
			if (cboChon.SelectedIndex ==1)text = "Quý " + cboQuy.Text + " Năm " + s_yyyy; 
			frmReport_kp1_class accMyform=new frmReport_kp1_class();
			if(!b_thang)
				if(s_tam!=cboThang.SelectedItem.ToString())
				{
					TinhCongThuc(mm1,mm2,s_yy);
					s_tam=cboThang.SelectedItem.ToString();
				}
			if(!b_quy)
				if(s_tam!=cboQuy.SelectedItem.ToString())
				{
					TinhCongThuc(mm1,mm2,s_yy);
					s_tam=cboQuy.SelectedItem.ToString();
				}
			if(!b_nam)
				if(s_tam!=numNam.Value.ToString())
				{
					TinhCongThuc(mm1,mm2,s_yy);
					s_tam=numNam.Value.ToString();
				}
			string idm=cbobaocao.SelectedValue.ToString();	
			long l_idm=long.Parse(a.getrowbyid(a.get_data("select * from "+a.user+".a_dmmaubc where maubieu='"+idm+"'").Tables[0],"maubieu='"+idm+"'")["id"].ToString().Trim());
			ds_Data = accMyform.getData_1(a,l_idm,s_Tenform,s_yy);			
			try
			{
				if (ds_Data==null) MessageBox.Show("Không có số liệu !","Thông báo !");
				else
				{	
					switch(loai)
					{
						case 1:
							frmReport f=new frmReport(a,ds_Data,s_Report,ngayin,Madvqhns,text,text1);
							f.ShowDialog();
							break;
						case 2:
                            //                            if(cbobaocao.SelectedValue.ToString()=="B09_DN")
                            //                                f_Excel(ds_Data);
                            //                            else 
                            //                            {
                            //string s_ExcelError="";								
                            //s_ExcelError=exportExcel.expExcel(a,ds_Data,"",ngaythangnam,"","","","","","","","","","","",s_Report.ToString());
                            //if (s_ExcelError!="")
                            //    MessageBox.Show(s_ExcelError,"Thông báo!");
                            //                            }
							break;
						default:
							break;
					}
				}
			}
			catch (Exception Ex)
			{
				MessageBox.Show("Error : " + Ex ,"Thông báo !");
			}
		}


		private void TinhCongThuc(string mm1,string mm2,string s_yy)
		{
			//xoa du lieu cu cua bang a_sdr truoc khi dien du lieu moi
			string sql1 = "truncate table " + a.user + ".a_sdr";
			a.execute_data(sql1);
			//xoa du lieu cu cua ban a_sdr truoc khi dien du lieu moi
			string sql2 = "truncate table " + a.user + ".a_sdrct";
			a.execute_data(sql2);

			frmReport_kp1_class accMyform=new frmReport_kp1_class();
			accMyform.a_sdr_sd(a,mm1,mm2,s_yy,i_userid);
			accMyform.a_fbr(a,mm1,mm2,s_yy,i_userid);
			a.get_data("update "+a.user+".a_congthuc set stien=0");
			accMyform.a_congthuc(a);
		}

		private void cboChon_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			try
			{
				switch (cboChon.SelectedIndex)
				{
					case 0:
						lblThoiGian.Text = "Tháng";
						lblThoiGian.Enabled = true;
						cboThang.Visible=true;
						cboThang.Enabled=true;
						cboQuy.Visible=false;
						numNam.Visible=true;						
						cboThang.Text= DateTime.Now.Month.ToString().PadLeft(2,'0');						
						break;
					case 1:
						lblThoiGian.Text = "Quý";
						lblThoiGian.Enabled = true;
						cboThang.Visible=false;
						cboQuy.Enabled=true;
						cboQuy.Visible=true;
						numNam.Visible=true;
						int i_Quy=(int)((int.Parse(DateTime.Now.Month.ToString())-1)/3)+1;
						cboQuy.Text=Convert.ToString(i_Quy);						
						break;
					case 2:
						lblThoiGian.Text = "Tháng";
						lblThoiGian.Enabled=false;
						cboThang.Visible=true;
						cboThang.Enabled=false;
						cboQuy.Visible=false;
						numNam.Visible=true;	
						numNam.Value=DateTime.Now.Year;						
						break;
					default:
						cboThang.Visible=false;
						cboQuy.Visible=false;
						numNam.Visible=false;
						break;
				}
			}
			catch (Exception Ex)
			{
				MessageBox.Show("Error : " + Ex ,"Thông báo !");
			}
		}

		private void cbobaocao_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			loadform();
		}

		private void butExcel_Click(object sender, System.EventArgs e)
		{
			Display(2);
		}

		private void butXem_Click(object sender, System.EventArgs e)
		{
			Display(1);
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
	}
}
